// 主界面
;
define(function(require, exports, module) {
	'use strict';
	var util = require("util");
	var base = require("base");
	var leftMenu = require("../leftMenu/leftMenu");
	var topMenu = require("../topMenu/topMenu");
	var footer = require("../footer/footer_main");
	var router = require("../hash/pageRouter");
	var dialog = require("dialog");
	var validate = require("validate");
	var tools = require("tools");
	var databind = require("databind");
	var css_select_insurance = require("./selectInsuranceVariety.css");
	var html_select_insurance = require("./html_select_insurance.html");
	var insuranceId;
	var financeId;
	var init = function(page) {
		insuranceId = page.params.insuranceId;
		financeId = page.params.financeId;
		//				insuranceId = 3;
		//				financeId = 24;
		footer.init();
		$("#footer").removeClass("none");
		leftMenu.init(8);
		topMenu.init(6);
		router.init();
		$(".table-section").removeClass("none");
		$(".footer").removeClass("none");
		tools.loadList("inquireInsuranceDetail.json", $("#insuranceCoveragesBody"), $("#insuranceCoveragesTpl"), {
			insuranceId: insuranceId
		}, function() {

		});
		initEvent();
	};
	var initEvent = function() {
		//若未登录，则返回登陆页面
		if (base.getData("sid") == null || base.getData("sid") == "" || base.getData("sid") == undefined) {
			location.href = "../login/login.html";
		}

		//删除险种
		$("#selectInsuranceVariety").on("click", ".insuranceCoverageDelete", function() {
			var insuranceCoverageId = $(this).parents("tr").attr("insuranceCoverageId");
			dialog.showConfirmDialog("险种删除", "是否删除该条险种？", function() {
				deleteInsuranceCoverage(insuranceCoverageId);
			});
		});
		
		$("#selectInsuranceVariety").on("click", ".back", function(){
			router.route("insuranceManage/insuranceDetail.html?insuranceId="+insuranceId+"&financeId="+financeId);
		});

		//险种明细
		$("#selectInsuranceVariety").on("click", ".insuranceCoverageDetail", function() {
			var insuranceCoverageId = $(this).parents("tr").attr("insuranceCoverageId");
			$("#popdialog").html(html_select_insurance);
			dialog.showDialog($("#select_insurance_dialog"));
			tools.loadList("inquireInsuranceCoverageDetail.json", $("#select_insurance_dialog .dialog_content"), $("#dialog_content_detail"), {
				insuranceCoverageId: insuranceCoverageId
			}, function() {
				$("#select_insurance_dialog").on("click", ".sure_yes", function() {
					dialog.hideDialog($("#select_insurance_dialog"));
				});
				$("#select_insurance_dialog .sure_no").remove();
			});
		});


		//添加险种
		$("#selectInsuranceVariety").on("click", ".addInsuranceVariety", function() {
			$("#popdialog").html(html_select_insurance);
			dialog.showDialog($("#select_insurance_dialog"));
			tools.requireInput($("#select_insurance_dialog"));
			databind.fillForm($("#select_insurance_dialog"), {
				insuranceId: insuranceId
			});

			tools.loadList("inquireFinanceApplicationDetail.json", $("#selectInsuranceTable"), $("#selectInsuranceTpl"), {
				financeId: financeId
			}, function() {
				$(".bulkstockDelete").unbind("click").click(function() {
					var $this = $(this);
					if ($this.parents("table").find("tr").length <= 2) {
						$this.parents(".dialogDataLine").remove();
						$(".bulkstockTitle").remove();
//						base.alert(3, "至少要留一条货物信息！");
					} else {
						$this.parents("tr").remove();
					}
				});
			});
			tools.loadList("inquireBulkstockInventoryPageList.json", $("#selectInsuranceBulkstockListingBody"), $("#selectInsuranceBulkstockListingTpl"), {
				financeId: financeId
			}, function() {
				$(".inventoryDelete").unbind("click").click(function() {
					var $this = $(this);
					if ($this.parents("table").find("tr").length <= 2) {
						$this.parents(".dialogDataLine").remove();
						$(".inventoryTitle").remove();
						$this.parents("tr").remove();
					}
				});
				$("#createInsurance").on("click", ".inventoryDelete", function() {
					$(this).parents("tr").remove();
					if ($("#createInsuranceBulkstockListingBody").find("tr").length <= 1) {
						$("#createInsuranceBulkstockListingBody").remove();
					}
				});
			});

			$("#select_insurance_dialog").on("click", ".sure_yes", function() {
				databind.submitDom($("#select_insurance_dialog"), function() {
					base.alert(1, "添加成功！");
					dialog.hideDialog($("#select_insurance_dialog"));
					router.route("insuranceManage/selectInsuranceVariety.html?insuranceId=" + insuranceId + "&financeId=" + financeId);
				});
			});

			$("#select_insurance_dialog").on("click", ".sure_no", function() {
				dialog.hideDialog($("#select_insurance_dialog"));
			});
		});
	};

	//删除险种
	var deleteInsuranceCoverage = function(insuranceCoverageId) {
		base.fnAjax({
			action: "deleteInsuranceCoverage.json",
			oData: {
				insuranceCoverageId: insuranceCoverageId,
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					base.alert(1, "删除成功！");
					router.route("insuranceManage/selectInsuranceVariety.html?insuranceId=" + insuranceId + "&financeId=" + financeId);
				} else {}
			}
		});
	};

	return {
		init: init
	};
});